﻿Imports WeifenLuo.WinFormsUI.Docking
Imports System.ComponentModel

Public Class ListWallpaperForm
  Inherits DockContent

  Public _windows As New Hashtable
  Private _bgworker As BackgroundWorker

  Public Sub LoadList(ByVal filter As String, Optional ByVal filtername As String = "Filter")
    Cursor = Cursors.WaitCursor
    MainTreeView.Nodes.Clear()
    Me.TabText = "Skins Übersicht"
    MainTreeView.Nodes.Add("Aktualisiere...").ForeColor = Color.Red
    Dim args(2) As String
    Refresh()
    _bgworker = New BackgroundWorker
    AddHandler _bgworker.DoWork, AddressOf LoadListThread
    AddHandler _bgworker.ProgressChanged, AddressOf BgWorkerProgressChanged
    args(0) = filter
    args(1) = filtername
    _bgworker.WorkerReportsProgress = True
    _bgworker.RunWorkerAsync(args)

    Cursor = Cursors.Default
  End Sub


  Public Sub LoadListThread(ByVal sender As Object, ByVal e As DoWorkEventArgs) '(ByVal filter As String, Optional ByVal filtername As String = "Filter")
    Dim filter As String = DirectCast(e.Argument, String())(0)
    Dim filtername As String = DirectCast(e.Argument, String())(1)
    Dim liw() As ListInfoWallpaper = ServiceManager.GetServiceManager.GetWallpaperService.GetMainList(filter, filtername)

    Dim node As New TreeNode
    Dim id As Integer
    Dim name_subnode As new TreeNode()
    Dim name As String = "start"

    Dim tv As New TreeView

    tv.Nodes.Clear()

    For i As Integer = 0 To liw.Length - 1 Step 1
      With liw(i)
        If id <> .SortNumber Then
          node = tv.Nodes.Add(.SortName)
          id = .SortNumber
          If .SortNumber = -1 Then
            node.ForeColor = Color.Green
          End If
          name = "neustart"
        End If
        If .NameSort = True Then
          If name <> .FullName Then
            name = .FullName
            name_subnode = node.Nodes.Add(.FullName)
            name_subnode.Nodes.Add(.Display).Name = .ID
          Else
            name_subnode.Nodes.Add(.Display).Name = .ID
          End If
        Else
          node.Nodes.Add(.Display).Name = .ID
        End If
      End With
    Next
    tv.Sort()
    Try
      _bgworker.ReportProgress(1, tv)
    Catch ex As Exception
      Dim ie As IEnumerator = tv.Nodes.GetEnumerator
      MainTreeView.Nodes.Clear()
      While ie.MoveNext
        MainTreeView.Nodes.Add(DirectCast(ie.Current, TreeNode).Clone())
      End While
      MainTreeView.Refresh()
    End Try
  End Sub

  Private Sub ListWallpaperForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    LoadList(FilterToolStripTextBox.Text)
  End Sub

  Private Sub RefreshToolStripButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles RefreshToolStripButton.Click
    LoadList(FilterToolStripTextBox.Text)
  End Sub

  Private Sub BgWorkerRefresh(ByVal sender As Object, ByVal e As DoWorkEventArgs)

    'Threading.Thread.Sleep(200)
    'DirectCast(DirectCast(e.Argument, ArrayList)(0), DownloadabeWallpaper).DownloadFile()
    '_bgworkers(DirectCast(DirectCast(e.Argument, ArrayList)(1), Integer)).ReportProgress(1, DirectCast(DirectCast(e.Argument, ArrayList)(0), DownloadabeWallpaper))

    'LoadList(FilterToolStripTextBox.Text)

  End Sub


  Private Sub BgWorkerProgressChanged(ByVal sender As Object, _
  ByVal e As System.ComponentModel.ProgressChangedEventArgs)


    'Dim dw As DownloadabeWallpaper = DirectCast(e.UserState, DownloadabeWallpaper)
    'Dim lvi As ListViewItem = ListView1.Items.Add(dw.Filename)
    'lvi.SubItems.Add(dw.FullName)
    'lvi.SubItems.Add(dw.Resolution)
    'lvi.SubItems.Add(dw.URL)
    'Refresh()
    'ProgressBar1.Value += 1

    Dim ie As IEnumerator = DirectCast(e.UserState, TreeView).Nodes.GetEnumerator
    MainTreeView.Nodes.Clear()
    While ie.MoveNext
      MainTreeView.Nodes.Add(DirectCast(ie.Current, TreeNode).Clone())
    End While

    'Me.MainTreeView = DirectCast(e.UserState, TreeView)
    'MainTreeView.Controls.Add(Me)
    'MainTreeView.Location = New Point(0, 26)
    'MainTreeView.Size = New Size(Me.Width, Me.Height - 26)

  End Sub

  Private Sub ListWallpaperForm_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
    MainTreeView.Location = New Point(0, 26)
    MainTreeView.Size = New Size(Me.Width, Me.Height - 26)
  End Sub

  Private Sub MainTreeView_AfterSelect(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles MainTreeView.AfterSelect
    Try
      Dim i As Integer = Integer.Parse(e.Node.Name)
      Dim w As Wallpaper = ServiceManager.GetServiceManager.GetWallpaperService.GetWallpaperById(e.Node.Name)
      w.AddListener(MdiMain._ipf)
      w.AddListener(MdiMain._pf)
      w.notifyChanges()
      Me.BringToFront()
      Me.Focus()
      Me.MainTreeView.SelectedNode = e.Node
    Catch ex As Exception

    End Try
  End Sub

  Private Sub MainTreeView_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MainTreeView.KeyDown
    If e.KeyCode = Keys.Enter Then
      If MainTreeView.SelectedNode.Name <> "" Then
        Dim w As Wallpaper = ServiceManager.GetServiceManager.GetWallpaperService.GetWallpaperById(MainTreeView.SelectedNode.Name)
        Dim f As New ImageViewForm
        f.MdiParent = MdiMain
        f.Show(MdiMain.DockPanel1, w)
      End If
    End If
  End Sub

  Private Sub MainTreeView_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MainTreeView.MouseClick
    If e.Button = Windows.Forms.MouseButtons.Right Then
      ContextMenuStrip1.Show(MainTreeView, e.X, e.Y)
    End If
  End Sub


  Private Sub MainTreeView_NodeMouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeNodeMouseClickEventArgs) Handles MainTreeView.NodeMouseDoubleClick
    If e.Node.Name <> "" Then
      Dim w As Wallpaper = ServiceManager.GetServiceManager.GetWallpaperService.GetWallpaperById(e.Node.Name)
      Dim f As New ImageViewForm
      f.MdiParent = MdiMain
      f.Show(MdiMain.DockPanel1, w)
    End If
  End Sub

  Private Sub FilterToolStripTextBox_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles FilterToolStripTextBox.GotFocus
    FilterToolStripTextBox.ForeColor = Color.Black
  End Sub

  Private Sub FilterToolStripTextBox_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles FilterToolStripTextBox.KeyDown
    If e.KeyCode = Keys.Enter Then
      LoadList(FilterToolStripTextBox.Text)
    End If
  End Sub

  Private Sub FilterToolStripTextBox_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles FilterToolStripTextBox.LostFocus
    FilterToolStripTextBox.ForeColor = Color.Gray
  End Sub

  Private Sub ListWallpaperForm_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.GotFocus
    MdiMain.Text = "Wallpaper Manager - " & Me.TabText
  End Sub

  Private Sub ListWallpaperForm_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LostFocus
    MdiMain.Text = "Wallpaper Manager"
  End Sub

  Private Sub AnzeigenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AnzeigenToolStripMenuItem.Click
    If MainTreeView.SelectedNode.Name <> "" Then
      Dim w As Wallpaper = ServiceManager.GetServiceManager.GetWallpaperService.GetWallpaperById(MainTreeView.SelectedNode.Name)
      Dim f As New ImageViewForm
      f.MdiParent = MdiMain
      f.Show(MdiMain.DockPanel1, w)
    End If
  End Sub

  Private Sub DiawhowToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DiawhowToolStripMenuItem.Click
    If MainTreeView.SelectedNode.Name = "" Then
      Cursor = Cursors.WaitCursor
      Dim i As New List(Of Integer)
      Dim tnc As TreeNodeCollection = MainTreeView.SelectedNode.Nodes
      For Each tn As TreeNode In tnc
        If tn.Name <> "" Then
          i.Add(Integer.Parse(tn.Name))
        Else
          For Each t As TreeNode In tn.Nodes
            i.Add(Integer.Parse(t.Name))
          Next
        End If
      Next
      Dim dsf As New DiaShowForm()
      dsf.showdialog(i)
      dsf.Dispose()
      Cursor = Cursors.Default
    End If
  End Sub
  
  Private Sub FormClosing_Eventhandler(sender As Object, e As FormClosingEventArgs) handles mybase.FormClosing 
  	e.Cancel = true 
  End Sub
  
End Class